import { createStore } from 'vuex'
import createPersistedState from "vuex-persistedstate"

export default createStore({
    plugins: [createPersistedState({
        storage: window.sessionStorage
    })],
    state: {
        userInfo: [],
        routerInfo: [],
        currentMenu: null,
        tabsList: [
            {
                path: '/children',
                name: '/children',
                label: 'Top',
            }
        ]
    },
    mutations: {
        saveUserInfo(state, arr) {
            state.userInfo = arr
        },
        saveRouterInfo(state, obj) {
            state.routerInfo = obj
        },
        saveTags(state, obj) {
            state.tabsList=obj
         },
        //选择标签 选择面包屑
        selectMenu(state, val) {
            if (val.name === '/children') {
                state.currentMenu = null
            } else {
                state.currentMenu = val
                //如果等于-1说明tabsList不存在那么插入，否则什么都不做
                let result = state.tabsList.findIndex(item => item.name === val.name)
                result === -1 ? state.tabsList.push(val) : ''

            }
            // val.name === 'home' ? (state.currentMenu = null) : (state.currentMenu = val)
        },
        //关闭标签
        closeTab(state, val) {
            let result = state.tabsList.findIndex(item => item.name === val.name)
            state.tabsList.splice(result, 1)
        },
    },
    actions: {
    },
    getters: {
        showId(state) {
            return state.userInfo.userName
        },
        showAuth(state) {
            return state.userInfo.authority
        },
        showuserGuid(state) {
            return state.userInfo.userinfoGuid
        },
        showuserInfo(state) {
            return state.userInfo
        },
        showentergroup(state) {
            return state.userInfo.groupEnterpriseGuid
        },
        showRouterInfo(state) {
            return state.routerInfo
        }
    }
})
